package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        //对密码进行md5加密，然后再进行比对
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        System.out.println(password);
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;
    }

    /*
     * 新增员工
     * */
    @Override
    public void addEmp(EmployeeDTO employeeDTO) {
        /*封装employee对象*/
        Employee employee = new Employee();
        /*拷贝部分信息*/
        BeanUtils.copyProperties(employeeDTO, employee);
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setStatus(StatusConstant.ENABLE);
/*        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        *//*从ThreadLocal里面获取当前登陆员工的id*//*
        Long empId = BaseContext.getCurrentId();
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);*/

        //调用mapper接口
        employeeMapper.insertEmp(employee);
    }

    /*
     * 员工分页查询
     * */
    @Override
    public PageResult findEmpByPage(EmployeePageQueryDTO employeePageQueryDTO) {
        //开启分页查询
        PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());
        Page<Employee> page = employeeMapper.selectEmpByPage(employeePageQueryDTO);
        long total = page.getTotal();
        List<Employee> records = page.getResult();

        //封装PageResult对象返回
        return new PageResult(total, records);

    }

    /*
    * 员工状态设置
    * */
    @Override
    public void setEmpStatus(Integer status, Long id) {
        //封装员工对象
        Employee employee = new Employee();
        employee.setId(id);
        employee.setStatus(status);
/*        employee.setUpdateTime(LocalDateTime.now());*/

        employeeMapper.updateStatus(employee);
    }

    /*
    * 根据id查询员工信息
    * */
    @Override
    public Employee findEmpById(Long id) {
        Employee employee = employeeMapper.selectEmpById(id);
        return employee;
    }

    /*
    * 修改员工信息
    * */
    @Override
    public void updateEmp(EmployeeDTO employeeDTO) {
        //封装employee对象
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
/*        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(BaseContext.getCurrentId());*/
        /*employee.setPassword("****");*/

        employeeMapper.updateStatus(employee);
    }

    /*
    * 修改员工密码
    * */
    @Override
    public void updateEmpPassword(PasswordEditDTO passwordEditDTO) {
        //获取当前登陆员工的id
        Long empId = BaseContext.getCurrentId();
        Employee employee = employeeMapper.selectEmpById(empId);
        if(employee == null){
            throw new PasswordErrorException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //获取该员工旧密码
        String oldPassword = employee.getPassword();
        String oldPasswordDTO = DigestUtils.md5DigestAsHex(passwordEditDTO.getOldPassword().getBytes());
        String newPasswordDTO = DigestUtils.md5DigestAsHex(passwordEditDTO.getNewPassword().getBytes());

        //判断旧密码与员工密码不一致
        if(!oldPassword.equals(oldPasswordDTO)){
            throw new PasswordErrorException(MessageConstant.PASSWORD_EDIT_FAILED);
        }

        //判断输入新密码是否与旧密码一致
        if(oldPassword.equals(newPasswordDTO)){
            throw new PasswordErrorException(MessageConstant.PASSWORD_IS_EQUAL);
        }

        employee.setPassword(newPasswordDTO);
        employeeMapper.updateStatus(employee);
    }

}
